﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ModBot.ConfigBuilder.CustomControls
{
    public partial class ClassPriorityControl : UserControl
    {
        public ClassPriorityControl()
        {
            InitializeComponent();
            InitClasses();
        }

        private void InitClasses()
        {
            healClassPriorityList.Items.Clear();


            string[] classes = Enum.GetNames(typeof(SharedLib.ClassName));
            healClassPriorityList.Items.AddRange(classes);

            SharedLib.UncheckListboxItems(healClassPriorityList);
        }

        public string GetClassPriority(char delimeter)
        {
            string classPriority = string.Empty;
            for (int i = 0; i < healClassPriorityList.CheckedItems.Count; i++)
            {
                string checkedItemText = healClassPriorityList.CheckedItems[i].ToString();

                if (classPriority.Length > 0) classPriority += delimeter;
                classPriority += checkedItemText;

            }

            return classPriority.ToLower();
        }
        

        public void SetClassPriority(string priorityList, char delimiter)
        {
            string classPriority = priorityList;

            if (classPriority != null && classPriority.Length > 0)
            {
                classPriority = classPriority.Replace("[", "").Replace("]", "").Replace(" ", "");

                string[] classPriorityArray = classPriority.Split(delimiter);

                for (int i = 0; i < classPriorityArray.Length; i++)
                {
                    for (int j = 0; j < healClassPriorityList.Items.Count; j++)
                    {
                        string listValue = healClassPriorityList.Items[j].ToString().ToUpper();
                        string searchValue =
                            Enum.GetName(typeof(SharedLib.ClassShortName),
                            SharedLib.ConvertClassShortNameToName(
                                        (SharedLib.ClassShortName)Enum.Parse(typeof(SharedLib.ClassShortName),
                                        classPriorityArray[i].ToUpper()))).ToUpper();


                        if (listValue == searchValue)
                        {
                            healClassPriorityList.Items.RemoveAt(j);
                            break;
                        }
                    }

                    SharedLib.ClassShortName shortName = (SharedLib.ClassShortName)Enum.Parse(typeof(SharedLib.ClassShortName), classPriorityArray[i].ToUpper());
                    SharedLib.ClassName className = SharedLib.ConvertClassShortNameToName(shortName);

                    healClassPriorityList.Items.Insert(i,
                        className);
                    healClassPriorityList.SetItemChecked(i, true);
                }
            }
        }


        private void healClassPriorityListUpButton_Click(object sender, EventArgs e)
        {
            if (healClassPriorityList.SelectedItem == null) return;

            Boolean isChecked = healClassPriorityList.GetItemChecked(healClassPriorityList.SelectedIndex);
            SharedLib.MoveSelectedListBoxItemUp(healClassPriorityList);
            healClassPriorityList.SetItemChecked(healClassPriorityList.SelectedIndex, isChecked);
        }

        private void healClassPriorityListDownButton_Click(object sender, EventArgs e)
        {
            Boolean isChecked = healClassPriorityList.GetItemChecked(healClassPriorityList.SelectedIndex);
            SharedLib.MoveSelectedListBoxItemDown(healClassPriorityList); 
            healClassPriorityList.SetItemChecked(healClassPriorityList.SelectedIndex, isChecked);
        }


    }
}
